package com.kaichaohulian.baocms.activity;


import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.adapter.HelpSongDetailAdapter;
import com.kaichaohulian.baocms.app.MyApplication;
import com.kaichaohulian.baocms.base.BaseMyActivity;
import com.kaichaohulian.baocms.ecdemo.common.utils.LogUtil;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.newmodel.IndexSongDetailModel;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.view.FullyGridLayoutManager;
import com.kaichaohulian.baocms.view.ShowDialog;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.kaichaohulian.baocms.R.id.text_ren;

public class HelpSongActivity extends BaseMyActivity {

    int userID;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.exit_layout)
    LinearLayout exitLayout;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.center_title_tv)
    TextView centerTitleTv;
    @BindView(R.id.navigation_bar)
    RelativeLayout navigationBar;
    @BindView(R.id.img_xu)
    ImageView imgXu;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_status)
    TextView textStatus;
    @BindView(R.id.text_time)
    TextView textTime;
    @BindView(R.id.text_static)
    TextView textStatic;
    @BindView(R.id.text_choulao)
    TextView textChoulao;
    @BindView(R.id.user_lianxir)
    TextView userLianxir;
    @BindView(R.id.text_phone)
    TextView textPhone;
    @BindView(R.id.text_xuqiushuoming)
    TextView textXuqiushuoming;
    @BindView(R.id.recycle_pic)
    RecyclerView recyclePic;
    @BindView(R.id.text_shouhuoren)
    TextView textShouhuoren;
    @BindView(R.id.textshouhuolianxi)
    TextView textshouhuolianxi;
    @BindView(R.id.text_shouhuo_addr)
    TextView textShouhuoAddr;
    @BindView(text_ren)
    TextView textRen;
    @BindView(R.id.text_shenqing)
    TextView textShenqing;
    @BindView(R.id.title_linLayout)
    LinearLayout titleLinLayout;
    @BindView(R.id.text_quhuo)
    TextView text_quhuo;

    String type = "1";
    @BindView(R.id.text_address)
    TextView text_address;

    public boolean flag ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_song);
        ButterKnife.bind(this);

        userID = MyApplication.getInstance().UserInfo.getUserId();
        type = getIntent().getStringExtra("type");
        if (type.equals("1")) {
            text_address.setText("买货地址:");
            imgXu.setImageResource(R.mipmap.mai);
        } else if (type.equals("2")) {
            text_address.setText("取货地址:");
            imgXu.setImageResource(R.mipmap.qu);
        } else {
            text_address.setText("送货地址:");
            imgXu.setImageResource(R.mipmap.song);
        }
        recyclePic.setLayoutManager(new FullyGridLayoutManager(HelpSongActivity.this, 3));

        getnewXuqiu();
    }

    List<String> list = new ArrayList<>();
    HelpSongDetailAdapter helpSongDetailAdapter;

    public void getnewXuqiu() {

        final RequestParams params = new RequestParams();
        params.put("userId", userID);
        params.put("demandId", getIntent().getStringExtra("demandId"));

        HttpUtil.post(Url.detail_xuqiu, params, new JsonHttpResponseHandler() {
            @Override
            public void onStart() {
                showLoading();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d(response.toString(), "333333333333333");
//                XuqiuDetailModel
                IndexSongDetailModel xuqiuDetailModel = new IndexSongDetailModel();
                Gson gson = new Gson();

                xuqiuDetailModel = gson.fromJson(response.toString(), IndexSongDetailModel.class);
                list = xuqiuDetailModel.getDataObject().getPictures();
                textTime.setText(xuqiuDetailModel.getDataObject().getCreatedTime());
                helpSongDetailAdapter = new HelpSongDetailAdapter(list);
                recyclePic.setAdapter(helpSongDetailAdapter);

                helpSongDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                        ImagePagerActivity.startImagePagerActivity(HelpSongActivity.this, list, position, imageSize);


                    }
                });


                textTitle.setText(xuqiuDetailModel.getDataObject().getTitle());
                textXuqiushuoming.setText("送货相关说明：" + xuqiuDetailModel.getDataObject().getRemark());
                //给人打电话的吧
                textRen.setText(xuqiuDetailModel.getDataObject().getPhoneNumber());
                textChoulao.setText(xuqiuDetailModel.getDataObject().getReward());
                textStatic.setText(xuqiuDetailModel.getDataObject().getKeyWord());
//取货联系人user_lianxir
                userLianxir.setText(xuqiuDetailModel.getDataObject().getGoodsUser());
//取货人的电话
                textPhone.setText(xuqiuDetailModel.getDataObject().getGoodsNumber());
                //取货地址
                text_quhuo.setText(xuqiuDetailModel.getDataObject().getGoodsAddress());
//收货联系人text_shouhuoren
                textShouhuoren.setText(xuqiuDetailModel.getDataObject().getReceiver());
//收货联系电话textshouhuolianxi
                textshouhuolianxi.setText(xuqiuDetailModel.getDataObject().getReceiverNumber());
                //收货地址text_shouhuo_addr
                textShouhuoAddr.setText(xuqiuDetailModel.getDataObject().getReceiverAddress());
                flag=xuqiuDetailModel.getDataObject().isApplyflag();

                if (xuqiuDetailModel.getDataObject().isApplyflag() == false) {
                    textShenqing.setText("未申请");
                    textStatus.setText("未申请");
                    type2 = 0;
//                    textShenqing.setEnabled(false);
                } else {
                    textShenqing.setText("申请");
//                    textShenqing.setEnabled(true);
                    textStatus.setText("取消申请");
                    type2 = 1;
                }
             /*   if (type2 == 1) {
                    textStatus.setText("未申请");
                } else {
                    textStatus.setText("已申请");
                }*/

            }

            @Override
            public void onFinish() {
                dismissLoading();
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();

            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.exit_layout, R.id.tv_title, R.id.center_title_tv, R.id.navigation_bar, R.id.img_xu, R.id.text_title, R.id.text_status, R.id.text_time, R.id.text_static, R.id.text_choulao, R.id.user_lianxir, R.id.text_phone, R.id.text_xuqiushuoming, R.id.recycle_pic, R.id.text_shouhuoren, R.id.textshouhuolianxi, R.id.text_shouhuo_addr, text_ren, R.id.text_shenqing, R.id.title_linLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.exit_layout:
                break;
            case R.id.tv_title:
                break;
            case R.id.center_title_tv:
                break;
            case R.id.navigation_bar:
                break;
            case R.id.img_xu:
                break;
            case R.id.text_title:
                break;
            case R.id.text_status:
                break;
            case R.id.text_time:
                break;
            case R.id.text_static:
                break;
            case R.id.text_choulao:
                break;
            case R.id.user_lianxir:
                break;
            case R.id.text_phone:
                break;
            case R.id.text_xuqiushuoming:
                break;
            case R.id.recycle_pic:
                break;
            case R.id.text_shouhuoren:
                break;
            case R.id.textshouhuolianxi:
                break;
            case R.id.text_shouhuo_addr:
                break;
            case text_ren:
                break;
            case R.id.text_shenqing:
                if (type2 == 0) {
                    showPopupWindow(textShenqing);
                } else {
                    getRequest();
                }


                break;
            case R.id.title_linLayout:
                break;
        }
    }

    private PopupWindow popWindow;

    private void showPopupWindow(View parent) {
        LogUtil.d("--> showPopupWindow");
        if (popWindow == null) {

            View view = LayoutInflater.from(this).inflate(R.layout.dialog_request, null);

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    EditText edit_shuoming;

    public void initPop(View view) {
        TextView btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);//拍照
        LinearLayout layout_dismiss = (LinearLayout) view.findViewById(R.id.layout_dismiss);
        layout_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        edit_shuoming = (EditText) view.findViewById(R.id.edit_shuoming);//拍照
        TextView btn_submit = (TextView) view.findViewById(R.id.btn_submit);//拍照
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                popWindow.dismiss();

            }
        });

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getRequest();
                popWindow.dismiss();

            }
        });

    }


    int type2 = 0;

    public void getRequest() {

        final RequestParams params = new RequestParams();
//        params.put("userId", getIntent().getStringExtra("fabuid"));
        params.put("userId",MyApplication.getInstance().UserInfo.getUserId());
        params.put("demandId", getIntent().getStringExtra("demandId"));
        if (flag == false){
            params.put("type", 0);
        }else {
            params.put("type", 1);
        }


        params.put("remark", edit_shuoming.getText().toString());
        //顺风车必填
        params.put("phone", "");

//        params.put("applyUserId", MyApplication.getInstance().UserInfo.getUserId());
        HttpUtil.post(Url.Index_Shenqing, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d(response.toString(), "333333333333333");

                if (response.optString("code").equals("1")) {
                    Toast.makeText(HelpSongActivity.this, response.optString("errorDescription"), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(HelpSongActivity.this, "申请成功", Toast.LENGTH_SHORT).show();

                    type2 = 0;
                    getnewXuqiu();
                }

//                Gson gson = new Gson();


            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();

            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });
    }

//    release_imageitem


}
